package com.scalar.chiptrack.operations.inventoryps.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.inventoryps.forms.InventoryPackingSlipForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;

public class InventoryPackingSlipLoadProcessTypesCommand extends ActionCommand {


    private static final String ACTION_NAME = "/inventoryps.do";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
    	InventoryPackingSlipForm inventoryPSForm = (InventoryPackingSlipForm) form;

        HashMap<String, String> errors 	 = new HashMap<String, String>();
        HashMap<String, String> info	 = (HashMap<String, String>) request.getAttribute( INFO_MESSAGES );
        if( info == null )
        {
        	info = new HashMap<String, String>();
        }
        String action = request.getParameter("action");
        
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );
        
        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if ( user.isActionAllowed( Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute( "showMessage", "TRUE" );
            }
        }

        inventoryPSForm.setProcessType( "0" );
        inventoryPSForm.setProcessStepId( 0 );
        inventoryPSForm.setLocationId( 0 );
        inventoryPSForm.setToVendorId( 0 );
        inventoryPSForm.setAvailableLotsList( new ArrayList<LotDetails>() );

        request.setAttribute( "processTypeList", NewProductDAO.loadAllProcessesFromDB( null, false ) );
        request.setAttribute( "action", action );
        
        return mapping.findForward( "showLots" );
    }
}
